package com.lcc.lynx.jdbc.handle;

import com.lcc.lynx.common.utils.DateUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

@MappedJdbcTypes(JdbcType.DATE)
@MappedTypes({Date.class, String.class})
public class Date2StringTypeHandler extends BaseTypeHandler<Serializable> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Serializable parameter, JdbcType jdbcType) throws SQLException {
        if (parameter instanceof Date d) {
            ps.setString(i, DateUtil.getCompact_DF(d));
        }
    }

    @Override
    public Serializable getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return null;
    }

    @Override
    public Serializable getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return null;
    }

    @Override
    public Serializable getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return null;
    }
}
